package inventoryServe.controller;


import com.alibaba.fastjson.JSONObject;
import inventoryServe.po.Role;
import inventoryServe.po.Store;
import inventoryServe.po.User;
import inventoryServe.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.Date;
import java.util.List;
import inventoryServe.utils.*;

import javax.servlet.http.HttpServletRequest;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-06-22
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;

// restful 风格的请求
//  url 不能有动词，只有复数名词

//    GET:      http://www.example.com/users		获取用户列表数据
//    POST:	    http://www.example.com/users		创建(添加)用户数据
//    GET:	    http://www.example.com/users/1		获取用户ID为1的用户信息
//    PUT:	    http://www.example.com/users/1		修改用户ID为1的用户信息
//    DELETE:	http://www.example.com/users/1		删除用户ID为1的用户信息


    // 获取用户列表数据
    @GetMapping("/users")
    @ResponseBody
    public Object allUser(HttpServletRequest req) {

        String username = req.getParameter("username").trim();
        String roleId = req.getParameter("roleId").trim();
        String storeId = req.getParameter("storeId").trim();
        String status = req.getParameter("status").trim();

        User user = new User();
        if (username != "") {
            user.setUsername(username);
        }
        if (roleId != "") {
            Role role = new Role();
            role.setId(Integer.parseInt(roleId));
            user.setRole(role);
        }
        if (storeId != "") {
            Store store = new Store();
            store.setId(Integer.parseInt(storeId));
            user.setStore(store);
        }
        if (status != "") {
            user.setIsEnable(Integer.parseInt(status));
        }
        List<User> res = this.userService.allUser(user);
        if (res != null) {
            return ResUtils.Success(1,"查询成功",res);
        } else {
            return ResUtils.Fail(0,"没有数据");
        }
    }

    // 根据 id 获取用户信息
    @GetMapping("/users/{id}")
    @ResponseBody
    public Object userOfId(@PathVariable("id") Integer id) {
        System.out.println("接收到请求");
        User res = userService.userOfId(id);
        if (res != null) {
            return ResUtils.Success(1,"查询成功",res);
        } else {
            return ResUtils.Fail(0,"没有数据");
        }
    }


    // 删除用户
    @DeleteMapping("users/{id}")
    @ResponseBody
    public Object deleteUser(@PathVariable("id") Integer id) {
        boolean res = userService.deleteUser(id);

        if (res) {
            return ResUtils.Success(1,"删除成功");
        } else {
            return ResUtils.Fail(0,"删除失败");
        }
    }

    // 增加用户
    @PostMapping("users/")
    @ResponseBody
    public Object addUser(@RequestBody User user) {
        System.out.println(user);
        System.out.println("4555555555555555");
        Date now = new Date();
        user.setCreateTime(now);
        user.setUpdateTime(now);
        System.out.println(user);
        boolean res = userService.addUser(user);

        if (res) {
            return ResUtils.Success(1,"添加成功",user);
        } else {
            return ResUtils.Fail(0,"添加失败");
        }
    }


    // 根据 id 修改用户信息
    @PutMapping("/users/{id}")
    @ResponseBody
    public Object editStudent(@PathVariable("id") Integer id,@RequestBody User user) {

        Date now = new Date();
        user.setUpdateTime(now);

        System.out.println(user);

        Boolean res = userService.editUser(user);

        if (res) {
            return ResUtils.Success(1, "修改成功",user);
        } else {
            return ResUtils.Fail(0, "修改失败");
        }
    }

    // 根据 id 修改状态用户信息
    @PutMapping("/users/status/{id}")
    @ResponseBody
    public Object isEnableUser(@PathVariable("id") Integer id,@RequestParam("isEnable") Integer isEnable) {
        System.out.println(id+"接收到的id " +isEnable);
        Integer flag = isEnable == 1 ? 0 : 1;
        Boolean res = userService.isEnableUser(id,flag);
        if (res) {
            return ResUtils.Success(1, "状态成功");
        } else {
            return ResUtils.Fail(0, "状态失败");
        }
    }


    // login

    // 登录
    @PostMapping("user/login")
    @ResponseBody
    public Object login(@RequestBody User user) {

        User res = userService.login(user);

        if (res != null) {
            return ResUtils.Success(1,"登录成功",res);
        } else {
            return ResUtils.Fail(0,"用户名或密码错误或账号被禁用");
        }
    }

    // 根据 获取验证码
    @GetMapping("/user/authCode/")
    @ResponseBody
    public Object getAuthCode(HttpServletRequest req) {
        String email = req.getParameter("email");
//
//        System.out.println("接收到请求");
//
//        System.out.println("开始发送邮箱了");

        String authCode = EmailUtil.getAuthCode();
        System.out.println("随机生成的6位验证码如下： "+authCode);

        JSONObject result = EmailUtil.sendAuthCodeEmail(email,authCode);

//        System.out.println(result);
//
//        System.out.println("发送邮箱了");

        return result;

    }

    // 根据 邮箱 修改 用户密码
    @PutMapping("/user/password/")
    @ResponseBody
    public Object editPass(@RequestBody User user) {
        System.out.println("141414444");
        System.out.println(user);
        Boolean res = userService.editPass(user);
        if (res) {
            return ResUtils.Success(1, "修改密码成功");
        } else {
            return ResUtils.Fail(0, "修改密码失败");
        }
    }



}

